/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cliente;

import cliente.auxiliar.Conexao;
import cliente.auxiliar.Envia;
import cliente.gui.Erro;
import cliente.gui.Login;
import cliente.gui.MyTwitter;
import cliente.gui.Register;
import cliente.rmi_aux.RMIRecebeTweets;
import cliente.rmi_aux.RMInterface;
import cliente.rmi_aux.ThreadPing;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

/**
 *
 * @author Daniel
 */
public class ClienteRMI {

    public static String mainHost = "localhost";
    public static int mainPort = 8000;

    public static void main(String args[]) throws NotBoundException, MalformedURLException, RemoteException {

        System.getProperties().put("java.security.policy", "policy.all");
        System.setSecurityManager(new RMISecurityManager());

        int tenta = 0;
        GereOperacoes gestor = new GereOperacoes();
        Login log = new Login(gestor);
        log.setVisible(true);
        arrancaLigacao(gestor, log, tenta);
    }

    public static void arrancaLigacao(GereOperacoes gestor, Login log, int tentativas) {

        System.out.println("No arranca ligacao!!!!");
        try {
            Conexao ligacao = new Conexao(mainHost, mainPort);
            
            if(ligacao.conexao_RMI()!=0/* && tentativas<20*/){
                log.setVisible(true);
                if(tentativas>20){
                    return;
                }
                System.out.println("A tentar establecer ligação!!!\n"+tentativas);
                gestor.getLogin().getLogButon().setEnabled(false);
                gestor.getLogin().getRegButon().setEnabled(false);
                dorme(1000);
                tentativas++;
                arrancaLigacao(gestor, log, tentativas);
            }

            else{
                gestor.setLogin(log);
                gestor.setConexao(ligacao);

                RMInterface inter = new RMInterface(gestor);
                RMIRecebeTweets recTwe = new RMIRecebeTweets(gestor);

                gestor.getLogin().getLogButon().setEnabled(true);
                gestor.getLogin().getRegButon().setEnabled(true);

                gestor.setInter(inter);
                gestor.setRecTwe(recTwe);
                Envia send = new Envia(ligacao, gestor);
                ThreadPing pingar = new ThreadPing(gestor);
                gestor.setPing(pingar);

                if (gestor.getMyTwi() == null) {
                    gestor.setMyTwi(new MyTwitter("", gestor));
                }
                gestor.setEnvia(send);

                if (gestor.getRegister() == null) {
                    gestor.setRegister(new Register(gestor));
                }
                gestor.setLogin(log);
                log.setVisible(true);
//                pingar.start();
                send.start();              
            }
       
        } catch (Exception e) {
            dorme(5000);

            System.out.println("[ERRO]1HOST:\n" + e.getMessage());

            log.setVisible(true);
            tentativas++;

            if (tentativas > 20) {
                new Erro("Conexao", "Incapaz de ligar ao servidor!Desligar");
                dorme(10000);
                gestor.getConexao().setEstado("Desligado");
                System.exit(0);
            } else {
                arrancaLigacao(gestor, log, tentativas);
            }
        }
    }

    public static synchronized void dorme(int tempo) {

        try {
            Thread.sleep(tempo);
        } catch (InterruptedException ex0) {
            System.out.println("[Erro]2:" + ex0.getMessage());
        }
    }
}
